package com.fhz.fengmvvm.test;

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModel;

import com.fhz.fengmvvm.user.Food;

/**
 * 时间:11/9/21
 *
 * @author msfeng
 * 简述: TODO
 */
public class MyViewModel extends ViewModel implements LifecycleObserver{

    private static final String TAG = "MyViewModel";
//    public int index = 0;

    public MutableLiveData<Integer> mutableLiveData =  new MutableLiveData<>();
    public MutableLiveData<Food> foodMutableLiveData =  new MutableLiveData<>();

    private MyResotiory myResotiory;

    public MyViewModel(int i) {
        mutableLiveData.postValue(i);
        myResotiory = new MyResotiory();
    }


    public void getFood(){
        Food food = myResotiory.getFood();
        foodMutableLiveData.postValue(food);
    }




    public void add(){
        if(mutableLiveData.getValue() == null){
            mutableLiveData.postValue(1);
        }else{
            Integer value = mutableLiveData.getValue();
            mutableLiveData.postValue(++value);
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        Log.i(TAG, "onResume: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        Log.i(TAG, "onStop: ");
    }
}
